package com.stephenone.zhdaily.storydetail;

import android.util.Log;

import com.stephenone.zhdaily.data.api.ZhiHuDailyApiHelper;
import com.stephenone.zhdaily.data.bean.NewsDetail;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/1/17.
 */

public class StoryDetailPresenter implements StoryDetailContract.Presenter {
    private static final String TAG = StoryDetailPresenter.class.getSimpleName();
    private StoryDetailContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private long mId;

    public StoryDetailPresenter(StoryDetailContract.View view,long id){
        mView = view;
        mCompositeDisposable = new CompositeDisposable();
        mView.setPresenter(this);
        mId = id;
    }

    @Override
    public void subscribe() {
        getStoryDetail();
    }

    private void getStoryDetail() {
        Log.e(TAG, "getStoryDetail: " + mId );
        mCompositeDisposable.add(ZhiHuDailyApiHelper.sService.getNewsDetail(mId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    showNewsDetail(result);
                }));
    }

    private void showNewsDetail(NewsDetail newsDetail) {
        mView.showNewsDetail(newsDetail);
    }

    @Override
    public void unsubscribe() {
        mCompositeDisposable.clear();
    }
}
